<div v-cloak id="app" class="main-content">
    <div class="form-body">
        <one-form :config="config" v-model="fdata" ref="ruleForm">
            <div slot="menuLeft">
                <el-button
                    class="button-item"
                    :loading="btnLoading"
                    type="primary"
                    @click="store('ruleForm')"
                    size="small"
                >
                    保存
                </el-button>
                <el-button size="small" @click="backpage()">返回</el-button>
            </div>
            <template
                slot="form-item-end"
                slot-scope="scope"
                v-if="scope.row.tip"
            >
                <span style="left: 400px; position: absolute; font-size: 12px">
                    {{scope.row.tip}}
                </span>
            </template>
        </one-form>
    </div>
</div>
{include file='common@components/one-form'} {include
file='common@components/one-upload'}
<script>
    const _formData = JSON.parse('{:json_encode((array)$formData, 1)}');
    const _salt = '{$salt}';
    var _groupData = JSON.parse('{:json_encode((array)$groupData, 1)}');
    const app = new Vue({
        el: '#app',
        data() {
            //自定义验证手机号
            var validatorMobile = (rule, value, callback) => {
                let myreg =
                    /^1([358][0-9]|4[1579]|66|7[0135678]|9[89])[0-9]{8}$/;
                if (value && !myreg.test(value)) {
                    callback(new Error('请输入正确的手机号'));
                } else {
                    callback();
                }
            };
            return {
                uploading: false,
                loading: false,
                btnLoading: false,
                config: {
                    formdesc: [
                        {
                            label: '会员组 :',
                            prop: 'group_id',
                            type: 'select',
                            rules: [
                                {
                                    required: true,
                                    message: '请选择会员所属分组',
                                },
                            ],
                            options: _groupData,
                        },
                        {
                            label: '会员昵称 :',
                            prop: 'nick',
                            type: 'input',
                            rules: [
                                { required: true, message: '会员昵称不能为空' },
                            ],
                            bind: {
                                placeholder: '请输入会员昵称',
                            },
                        },
                        {
                            label: '登陆账号 :',
                            prop: 'username',
                            type: 'input',
                            rules: [
                                { required: true, message: '登陆账号不能为空' },
                            ],
                            bind: {
                                placeholder: '请输入登陆账号',
                            },
                        },
                        {
                            label: '登陆密码 :',
                            prop: 'password',
                            type: 'input',
                            rules: [
                                { required: true, message: '登陆密码不能为空' },
                            ],
                            bind: {
                                placeholder: '请输入登陆密码',
                            },
                        },
                        {
                            label: '联系邮箱 :',
                            prop: 'email',
                            type: 'input',
                            rules: [
                                {
                                    type: 'email',
                                    message: '请输入正确的邮箱地址',
                                },
                            ],
                            bind: {
                                placeholder: '请输入联系邮箱',
                            },
                        },
                        {
                            label: '联系手机 :',
                            prop: 'mobile',
                            type: 'input',
                            rules: [{ validator: validatorMobile }],
                            bind: {
                                placeholder: '请输入联系手机',
                            },
                        },
                        {
                            label: '会员有效期 :',
                            prop: 'expire_time',
                            type: 'input',
                            bind: {
                                placeholder: '请输入会员有效期',
                            },
                            tip: '0为永久有效',
                        },
                        {
                            label: '会员状态 :',
                            prop: 'status',
                            type: 'radio',
                            options: [
                                {
                                    label: '正常',
                                    value: 1,
                                },
                                {
                                    label: '禁用',
                                    value: 0,
                                },
                            ],
                        },
                        {
                            label: '备注信息 :',
                            prop: 'remark',
                            type: 'input',
                            bind: {
                                placeholder: '帐号备注信息',
                            },
                        },
                    ],
                    labelWidth: '200px',
                    rowSize: 1, //一行可以展示几列表单，默认为3列
                },
                fdata: {
                    id: 0,
                    group_id: '',
                    nick: '',
                    username: '',
                    password: '',
                    email: '',
                    mobile: '',
                    remark: '',
                    expire_time: 0,
                    status: 1,
                    salt: _salt,
                },
            };
        },
        created() {},
        mounted() {
            if (Object.keys(_formData).length > 0) {
                let _this = this;
                _this.fdata = {
                    id: _formData.id,
                    group_id: _formData.group_id,
                    nick: _formData.nick,
                    username: _formData.username,
                    password: _formData.password,
                    email: _formData.email,
                    remark: _formData.remark,
                    mobile: _formData.mobile,
                    expire_time: _formData.expire_time,
                    status: _formData.status,
                    salt: _salt,
                };
            }
        },
        methods: {
            //表单验证
            getFormPromise(form) {
                return new Promise((resolve) => {
                    form.validate((res) => {
                        resolve(res);
                    });
                });
            },
            store(formName) {
                let _this = this;
                // 获取到组件中的form
                const configForm = this.$refs.ruleForm.$refs.ruleForm;
                // 使用Promise.all去校验结果,可加入多个表单
                Promise.all([configForm].map(this.getFormPromise)).then(
                    (res) => {
                        const validateResult = res.every((item) => !!item);
                        if (validateResult) {
                            _this.loading = true;
                            if (_this.fdata.password == '******') {
                                delete _this.fdata.password;
                                delete _this.fdata.salt;
                            }
                            request({
                                params: {
                                    s: 'user/index/edit',
                                },
                                method: 'post',
                                data: _this.fdata,
                            }).then((e) => {
                                _this.loading = false;
                                if (e.data.code == 0) {
                                    _this.backpage();
                                } else {
                                    _this.$message.error(e.data.msg);
                                }
                            });
                        } else {
                            console.log('表单未校验通过');
                        }
                    }
                );
            },
            backpage() {
                navigateTo({
                    s: 'user/index/index',
                });
            },
            //图片上传完成
            uploadSuccess(file, prop) {
                file = file.response.data.data.file;
                this.fdata[prop] = file;
            },
        },
    });
</script>
